<template>
    <div class="register">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="ruleForm2.name"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="ruleForm2.sex">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm2.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm2.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')" :loading='isRegister'>提交</el-button>
                <el-button @click="resetForm('ruleForm2')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { mapMutations, mapState } from "vuex";
import { ADD_USER } from "../mutation-types";
import axios from "axios";
import URL from "../serviceAPI.config";
export default {
  name: "Register",
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      } else {
        callback();
      }
    };
    var checkSex = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("性别不能为空"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      isRegister:false,
      ruleForm2: {
        pass: "",
        checkPass: "",
        name: "",
        sex: ""
      },
      rules2: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        sex: [{ validator: checkSex, trigger: "blur" }],
        name: [{ validator: checkName, trigger: "blur" }]
      }
    };
  },
  computed: {
    ...mapState(["isLoging", "userNames"])
  },
  methods: {
    ...mapMutations({
      add: "ADD_USER"
    }),
    submitForm(formName) {
      this.isRegister = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          let aryName = this.userNames.some(
            item => item.name == this.ruleForm2.name
          );
          if (aryName) {
            this.$message.error("账号已经存在了!!!");
            return false;
          }
          let obj = {
            id: this.userNames.length,
            userName: this.ruleForm2.name,
            password: this.ruleForm2.pass,
            sex: this.ruleForm2.sex,
            tag: this.ruleForm2.name == "admin" ? "管理员" : "普通用户"
          };
          axios
            .post(URL.userRegisterApi, obj)
            .then((response)=> {
              if (response.data.code == 200) {
                this.$message({
                  message: response.data.msg,
                  type: "success"
                });
                this.isLoging ? this.$router.push("/") : this.$router.push("/loging");
              }else{
                this.$message.error(response.data.msg);
                this.isRegister = false;
              }
            })
            .catch(function(error) {
              console.log(error);
               this.isRegister = false;
            });
          
        } else {
          console.log("error submit!!");
          return false;
           this.isRegister = false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped lang='scss'>
.register {
  width: 25%;
  height: 100%;
  margin: 100px auto;
}
</style>